﻿using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {

	//private Transform Player;
	private Transform MainCamera;
	private float Ymargin = 0.1f;
	Vector2 MaxXandY = new Vector2(13.0f, 18.7f);
	Vector2 MinXandY = new Vector2(13.0f, 7.3f);

	// Use this for initialization
	void Start () {
	//	Player = GameObject.FindGameObjectWithTag("Player").transform;

		MainCamera = Camera.main.transform;
	}

	bool checkYmargin(){
		//return Mathf.Abs(Player.position.y - transform.position.y)>Ymargin;
		return Mathf.Abs(MainCamera.position.y - transform.position.y)>Ymargin;
	}
	// Update is called once per frame
	void Update () {
		 trackPlayer();
	}
	void trackPlayer(){
		//float targetY = transform.position.y;
		float targetY = MainCamera.position.y;
		if(checkYmargin()){
			targetY = Mathf.Lerp(MainCamera.position.y,transform.position.y+3.5f	,Time.time);//加6是为了使坦克基本处于屏幕底部
		}
		targetY = Mathf.Clamp(targetY,MinXandY.y,MaxXandY.y);

		//transform.position = new Vector3(transform.position.x,targetY,transform.position.z);
		MainCamera.position = new Vector3(MainCamera.position.x,targetY,MainCamera.position.z);
	}

	void Test(){
		Debug.Log("Camera Test");
		
	}
}
